﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;

namespace TsWeb.Sports
{
    public partial class ManageSports : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindSports();
        }

        private void BindSports()
        {
            SportsGrid.DataSource = TsSystem.Instance.GetAllSports();
            SportsGrid.DataBind();
            SportsGrid.UseAccessibleHeader = true;
            if (SportsGrid.HeaderRow != null)
            {
                SportsGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
            }            
        }

        protected void ButtonCreateSport_Click(object sender, EventArgs e)
        {
            UserButton.Text = "Crear deporte";
            Session["Action"] = "Create";
            
            CleanUserForm();

            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "$('#myModal').modal('show');", true);
            //ModalPopupExtender1.Show();
        }

        protected void UpdateSportButton_Click(object sender, EventArgs e)
        {
            string opt = Session["Action"].ToString();
            switch (opt)
            {
                case "Create":
                    {
                        string sportName = SportNameTxt.Text;
                        TsSystem.Instance.CreateNewSport(sportName);
                        break;
                    }
                case "Update":
                    {                       
                        Sport updatedSport = (Sport)Session["UpdatedSport"];
                        updatedSport.Name = SportNameTxt.Text;
                        TsSystem.Instance.UpdateSport(updatedSport);
                        break;
                    }
                case "Delete":
                    {
                        break;
                    }
            }
            BindSports();
        }

        private void CleanUserForm()
        {
            SportNameTxt.Text = "";
            
        }

        protected void SportsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = SportsGrid.Rows[index];
            string SportName = row.Cells[0].Text;
            if (e.CommandName == "EditCommand")
            {
                Sport sport = TsSystem.Instance.GetSport(SportName);
                Session["UpdatedSport"] = sport;
                SportNameTxt.Text = SportName;
      

                UserButton.Text = "Actualizar deporte";
                Session["Action"] = "Update";
                //ModalPopupExtender1.Show();
                ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "$('#myModal').modal('show');", true);
            }
            if (e.CommandName == "DeleteCommand")
            {
                Sport sport = TsSystem.Instance.GetSport(SportName);
                Session["ToDeleteSport"] = sport;
                Session["Action"] = "Delete";

                Sport toDeleteSport = (Sport)Session["ToDeleteSport"];
                TsSystem.Instance.RemoveSport(toDeleteSport);
            }
        }
    }
}